<?php

class AdplaceAction extends CommonAction {

    // 查询数据
    public function index() {
		$adplacetypeHTML = $this->getAdplayType();
		$this->assign('adplacetypeHTML', $adplacetypeHTML);
        $this->display();
    }
	
	public function getAdplayType() {
		$adplacetype = D('AdplaceType')->where("status='0'")->order("sort")->select();
		
		if( $adplacetype ) {
			foreach( $adplacetype as $row ) {
				$adplacetypeSelect[] = '<option value="'.$row['id'].'">'.$row['name'].'</option>';
			}
		}
		return $adplacetypeSelect ? implode(',',$adplacetypeSelect) : '';
	}
	
	 public function indexjson() {
	 
		$page = max(1, intval( $_POST['page'] ) );
		$rows = intval( $_POST['rows'] ) ? intval( $_POST['rows'] ) : 20;
		$limitstart = $rows*($page-1);
		$Adplace = D("Adplace");
        $list = $Adplace->limit( $limitstart , $rows)->order('id desc')->select();
		$row_sum = $Adplace->count();
		
		
       
		$i = 0;
		foreach($list as $row ){
			$channelname = D("Channel")->where("id='{$row['channelid']}'")->field('channelname')->find();	
			$typename 	 = D('AdplaceType')->where("id='{$row['typeid']}'")->field('name')->find();
			$adplace[$i]['table_id'] = $row['id'];
			$adplace[$i]['table_name'] = $row['name'];
			$adplace[$i]['table_channel']['channelid'] = $row['channelid'];
			$adplace[$i]['table_channel']['channelname'] = $channelname['channelname'];
			$adplace[$i]['table_type']['typeid'] = $row['typeid'];
			$adplace[$i]['table_type']['typename'] = $typename['name'];
			$adplace[$i]['table_width'] = $row['width'];
			$adplace[$i]['table_height'] = $row['height'];
			$adplace[$i]['table_status'] = $row['status'];
			$i++;
			}
		$adplaceHtml['total'] = $row_sum['num'];
		$adplaceHtml['rows'] = $adplace;
		
		print_r( $adplaceHTML );
		echo json_encode($adplaceHtml);
		
    }

    // 写入数据
    public function add() {
        $Adplace = D("Adplace");
        if ($vo = $Adplace->create()) {
            $list = $Adplace->add();
            if ($list !== false) {
                $this->success('数据保存成功！');
            } else {
                $this->error('数据写入错误！');
            }
        } else {
            $this->error($Adplace->getError());
        }
    }

    // 更新数据
    public function edit() {
        $Adplace = D("Adplace");
        if ($vo = $Adplace->create()) {
            $list = $Adplace->save();
            if ($list !== false) {
                $this->success('数据更新成功！');
            } else {
                $this->error("没有更新任何数据!");
            }
        } else {
            $this->error($Adplace->getError());
        }
    }

    // 删除数据
    public function remove() {
		$id = intval( $_POST['form_id'] );
        if (!empty($id)) {
            $Adplace = M("Adplace");
            $result = $Adplace->delete($id);
            if (false !== $result) {
                $this->success('删除成功！');
            } else {
                $this->error('删除出错！');
            }
        } else {
            $this->error('ID错误！');
        }
    }
}
?>